﻿using System;
using System.Collections;
using System.Linq;
using System.IO;
using System.Windows.Forms;
using Common;
using WebServer;
using TemplateEngine;
using WebServer.Handlers;
using BusStopWebApp.Services;
using BusStopDataAccess;
using Common.Logging;

namespace BusStopWebApp
{
    static class Program
    {
        private static ILog log = LogManager.GetCurrentClassLogger();
        private static readonly FileInfo dbFileLocation = new FileInfo(Path.Combine(Application.StartupPath, "../../../../db/ottawa.db"));
        private static readonly String wwwRoot = Path.Combine(Application.StartupPath, "www");

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            if (!RunAsAdmin.Check())
            {
                log.Warn("Server was started with insufficient permissions");
                RunAsAdmin.Run(Application.ExecutablePath);
            }
            else
            {
                try
                {
                    HttpServer server = new HttpServer(8123);
                    server.AddDefaultHandler(new FilesHandler(wwwRoot));
                    server.AddHandler("stops", new RESTHandler(new BusStopLookup(dbFileLocation)));
                    server.Start();                    
                }
                catch (Exception e)
                { log.Error(e); }
            }
        }
    }
}
